#include "stdafx.h"
#include "ESGL.h"
CESGL::CESGL(void)
:m_nRequestedConfig(0)
{
}

CESGL::~CESGL(void)
{
}

bool CESGL::InitESGL( EGLNativeWindowType hWnd, EGLNativeDisplayType hDC, EGLNativePixmapType hBitmap )
{

	m_glNWT = hWnd;
	m_glNDT = hDC;
	m_glNPT = hBitmap;

	if ( m_glNWT == NULL )
	{
		printf("Init ESGL error : nwt is null\n ");
		return false;
	}

	m_glDisplay = eglGetDisplay( m_glNDT );
	if ( m_glDisplay == EGL_NO_DISPLAY )
	{
		printf("Using the default display type.\n");
		m_glDisplay = eglGetDisplay((EGLNativeDisplayType) EGL_DEFAULT_DISPLAY );
	}

	if ( !eglInitialize( m_glDisplay, &m_iMajorVersion, &m_iMinorVersion ))
	{
		printf("eglInitialize fail to do \n");
		return false;
	}
	
	do 
	{
		m_glConfig = ChooseConfigESGL();

		eglGetConfigAttrib( m_glDisplay, m_glConfig, EGL_CONFIG_ID, &m_nConfig );

		if ( m_glContext  )
		{
			eglDestroyContext( m_glDisplay, m_glContext );
		}

		EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };

		m_glContext = eglCreateContext( m_glDisplay, m_glConfig, EGL_NO_CONTEXT, contextAttribs );

		if ( m_glContext == EGL_NO_CONTEXT )
		{
			if ( m_nRequestedConfig > 0 )
			{
				return false;
			}
			else if( m_SESGL.bNeedPixmap )
			{
				m_SESGL.bNeedPixmap = false;
			}
			else if( m_SESGL.bNeedStencilBuffer )
			{
				m_SESGL.bNeedStencilBuffer = false;
			}
			else if ( m_SESGL.nFSAAMode > 0 )
			{
				m_SESGL.nFSAAMode = 0; 
			}
			else
			{
				printf("Create the context error!\n");
				return false;
			}
		}

	} while ( m_glContext == EGL_NO_CONTEXT );

	if ( m_glSurface )
	{
		eglDestroySurface( m_glDisplay, m_glSurface );
	}
	m_glSurface = eglCreateWindowSurface( m_glDisplay, m_glConfig, m_glNWT, NULL );

	if ( m_glSurface == EGL_NO_SURFACE  )
	{
		printf("create the surface error! \n" );
		return false;
	}

	if ( !eglMakeCurrent( m_glDisplay, m_glSurface, m_glSurface, m_glContext ) )
	{
		printf("egl make error\n");
		return false;
	}

	return true;
}

void CESGL::ReleaseESGL()
{	
	eglMakeCurrent(m_glDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
	eglDestroyContext(m_glDisplay, m_glContext);
	eglDestroySurface(m_glDisplay, m_glSurface);
	eglTerminate( m_glDisplay );
}

EGLConfig CESGL::ChooseConfigESGL()
{

	EGLint		nConfig;
	EGLConfig	conf = (EGLConfig) 0;
	EGLint		confList[32] = { 0 };
	int			i = 0;
	
	// do the 
	if ( m_nRequestedConfig > 0 )
	{
		confList[i++]	= EGL_CONFIG_ID;
		confList[i++]	= m_nRequestedConfig;
		confList[i]		= EGL_NONE;
		if ( !eglChooseConfig( m_glDisplay, confList, &conf, 1, &nConfig) || nConfig != 1 )
		{
			return 0;
		}
		return conf;
	}

	confList[i++] = EGL_BUFFER_SIZE;
	confList[i++] = m_SESGL.nColorBPP;

	if ( m_SESGL.bNeedZbuffer || m_SESGL.nDepthBPP > 0 )
	{
		confList[i++] = EGL_DEPTH_SIZE;
		confList[i++] = (m_SESGL.nDepthBPP > 0) ? m_SESGL.nDepthBPP : 16;
	}

	if ( m_SESGL.bNeedStencilBuffer )
	{
		confList[i++] = EGL_STENCIL_SIZE;
		confList[i++] = 8;
	}

	confList[i++] = EGL_SURFACE_TYPE;
	confList[i]	  = EGL_WINDOW_BIT;

	if ( m_SESGL.bNeedPBuffer )
	{
		confList[i] |= EGL_PBUFFER_BIT;
	}

	if ( m_SESGL.bNeedPixmap )
	{
		confList[i] |= EGL_PIXMAP_BIT;
	}

	++i;
	confList[i++] = EGL_RENDERABLE_TYPE;
	confList[i++] = EGL_OPENGL_ES2_BIT;

	switch ( m_SESGL.nFSAAMode )
	{
		case 1:
			confList[i++] = EGL_SAMPLE_BUFFERS;
			confList[i++] = 1;
			confList[i++] = EGL_SAMPLES;
			confList[i++] = 2;
			break;
		case 2:
			confList[i++] = EGL_SAMPLE_BUFFERS;
			confList[i++] = 1;
			confList[i++] = EGL_SAMPLES;
			confList[i++] = 4;
			break;
		default:
			confList[i++] = EGL_SAMPLE_BUFFERS;
			confList[i++] = 0;
			break;
	}

	confList[i++] = EGL_NONE;

	if(!eglChooseConfig(m_glDisplay, confList, &conf, 1, &nConfig) || nConfig != 1)
	{
		printf("Choose EGL Config Error!\n");
		return 0;
	}
	return conf;
}

void CESGL::CompleteDrawESGL()
{
	eglSwapBuffers( m_glDisplay, m_glSurface );
}